package com.trover.myapplication.ui.login

import androidx.lifecycle.Observer
import com.trover.myapplication.R
import com.trover.myapplication.core.ui.BaseVMActivity
import com.trover.myapplication.core.util.toast
import kotlinx.android.synthetic.main.activity_login.*
import org.koin.androidx.viewmodel.ext.android.getViewModel

class LoginActivity : BaseVMActivity<LoginViewModel>() {
    override fun initVM(): LoginViewModel = getViewModel()

    override fun startObserve() {
        mViewModel.apply {
            uiState.observe(this@LoginActivity, Observer { it ->
                when (it?.needLogin) {
                    true -> {
                        it.isError?.let {
                            toast(it.toString() + "")
                        }
                    }
                    else -> {
                        it?.isSuccess?.let {
                            toast("登录成功")
                            finish()
                        }
                    }
                }
            })
        }
    }

    override fun getLayoutResId(): Int = R.layout.activity_login

    override fun initView() {
        login.setOnClickListener {
            mViewModel.login(userNameEt.text.toString().trim(), passwordEt.text.toString().trim())
        }
    }

    override fun initData() {
    }
}